from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from apps.center.serializers import Userserializer, LabelSerializer
from apps.question.models import Label
from apps.users.models import User


class UserinfoListView(ViewSet):
    permission_classes = []


    def list(self, request):
        id = request.user.id
        obj = User.objects.get(id=id)
        serializer = Userserializer(obj)
        return Response(serializer.data)

    def put(self, request):
        data = request.data
        id = request.user.id
        obj = User.objects.get(id=id)
        serializer = Userserializer(instance=obj, data=data)
        serializer.is_valid()
        serializer.save()
        return Response(serializer.data)

    def avatar(self, request):
        data = request.data.get('img').read()
        # 创建fdfs的连接
        id = request.user.id
        from fdfs_client.client import Fdfs_client
        client = Fdfs_client('utils/fastdfs/client.conf')
        # 保存图片
        res = client.upload_by_buffer(data)
        if res['Status'] != 'Upload successed.':
            return Response(status=403)
            # 获取上传后的路径
        image_url = res['Remote file_id']
        # 保存地址
        # id = request.user.id
        # user = User.objects.get(id=1)
        # user.avatar = image_url
        # user.save()
        return Response({"imgurl": "http://192.168.19.128:8888/" + image_url})


    def password(self, request):
        data = request.data.get("password")
        id = request.user.id
        user = User.objects.get(id=id)
        user.set_password(data)
        user.save()
        return Response(status=200)


#  完
class LabelsAPIView(APIView):
    permission_classes = []

    def get(self, request):
        label = Label.objects.all()
        serializer = LabelSerializer(instance=label, many=True)
        return Response(serializer.data)

    def put(self, request):
        user = request.user
        id = request.data.get("labels")
        for i in id:
            obj = Label.objects.get(id=i)
            count = Label.objects.filter(users__in=(user,), id=i).count()
            if count != 0:
                obj.users.remove(user)
            else:
                obj.users.add(user)
        return Response(status=200)

# class AvatarView(APIView):
#     permission_classes = []
#
#     def post(self, request):                             ###############################################
#         data = request.data.get('img').read()
#         # 创建fdfs的连接                                   #######       作废             ################
#         from fdfs_client.client import Fdfs_client       ###############################################
#         client = Fdfs_client('utils/fastdfs/client.conf')
#         # 保存图片
#         res = client.upload_by_buffer(data)
#         if res['Status'] != 'Upload successed.':
#             return Response(status=403)
#             # 获取上传后的路径
#         image_url = res['Remote file_id']
#         # 保存地址
#         # id = request.user.id
#         user = User.objects.get(id=1)
#         user.avatar = image_url
#         user.save()
#         return Response({"imgurl": "http://192.168.19.128:8888/" + image_url})
#
#
#
#
# class PasswordView(APIView):
#     def put(self, request):
#         data = request.data.get("password")
#         user = User.objects.get(id=1)
#         user.set_password(data)
#         user.save()
#         return Response(status=200)
